import 'package:flutter/material.dart';
import 'package:lib_core/lib_core.dart';
import 'package:lib_ui/utils/ld_font.dart';
import 'package:lib_ui/widget/common_text_field.dart';
import 'package:lib_ui/widget/main_button.dart';
import 'package:lib_ui/widget/transparent_app_bar.dart';

import '../../logic/user_logic.dart';

class RegisterPage extends StatelessWidget {
  RegisterPage({super.key});

  String username = '';
  String password = '';
  String passwordAgain = '';
  late final logic = Get.put(UserLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TransparentAppBar(title: '注册'),
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            _buildInputAccount(),
            const SizedBox(height: 10),
            _buildInputPassword(),
            const SizedBox(height: 10),
            _buildInputPasswordAgain(),
            const SizedBox(height: 10),
            SizedBox(
              width: 100,
              child: MainButton(
                text: '注册',
                onClick: () async {
                  if (username.isEmpty || password.isEmpty || passwordAgain.isEmpty) {
                    '请输入数据'.showError();
                    return;
                  }
                  if (password != passwordAgain && password.isNotEmpty) {
                    '两次密码不对'.showError();
                    return;
                  }
                  final user = await logic.register(username, password, passwordAgain);
                  if (user != null) {
                    await '注册成功'.showSuccess();
                    BoostNavigator.instance.pop();
                  }
                },
              ),
            )
          ],
        ),
      ),
    );
  }

  _buildInputAccount() {
    return Row(
      children: [
        Text(
          '输入账号：',
          style: LDFont.regular(),
        ),
        CommonTextField(
          type: CommonTextFieldType.clear,
          onSubmitted: (str) {},
        ),
      ],
    );
  }

  _buildInputPasswordAgain() {
    return Row(
      children: [
        Text(
          '输入密码：',
          style: LDFont.regular(),
        ),
        CommonTextField(
          type: CommonTextFieldType.password,
          onSubmitted: (str) {},
        ),
      ],
    );
  }

  _buildInputPassword() {
    return Row(
      children: [
        Text(
          '确认密码：',
          style: LDFont.regular(),
        ),
        CommonTextField(
          type: CommonTextFieldType.password,
          onSubmitted: (str) {},
        ),
      ],
    );
  }
}
